package st.business.tepth.business.visitor.busnumber;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.business.bean.BusNumberBean;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.indicator.magicindicator.MagicIndicator;
import st.shadow.library.ui.indicator.magicindicator.ViewPagerHelper;
import st.shadow.library.ui.indicator.magicindicator.buildins.commonnavigator.CommonNavigator;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-03-05 17:24
 * 车辆编号列表
 */

public class BusNumberDelegate extends MvpFragment<BusNumberPresenter>
        implements BusNumberContract.View {
    //工位id
    private static final String LOCATION_ID = "id";

    public static BusNumberDelegate newInstance(int id) {
        Bundle bundle = new Bundle();
        bundle.putInt(LOCATION_ID, id);
        BusNumberDelegate delegate = new BusNumberDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.view_pager_bus_number)
    ViewPager mViewPagerBusNumber;
    @BindView(R2.id.indicator)
    MagicIndicator mMagicIndicator;
    @BindView(R2.id.layout_container)
    ConstraintLayout mLayoutContainer;
    private int mLocationId;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mLocationId = bundle.getInt(LOCATION_ID);
        }
    }

    @Override
    public BusNumberPresenter getPresenter() {
        return new BusNumberPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_visitor_bus_number;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        addTopView(R.layout.view_loading,mLayoutContainer);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        requestData();
    }
    private void requestData() {
        addTopView(R.layout.view_loading, mLayoutContainer);
        mPresenter.getBusList(mLocationId);
    }
    @Override
    public void showBusList(List<BusNumberBean> list) {
        if(list != null && !list.isEmpty()) {
            //显示第一个作业中界面
            int position = 0;
            for (int i = 0; i < list.size(); i++) {
                BusNumberBean busNumberBean = list.get(i);
                if (busNumberBean.getJob_process_status() == 2) {
                    position = i;
                    break;
                }
            }
            mViewPagerBusNumber.setAdapter(new BusNumberAdapter(getChildFragmentManager(),
                    list));
            CommonNavigator commonNavigator = new CommonNavigator(_mActivity);
            commonNavigator.setAdapter(new NumberNavigatorAdapter(_mActivity, mViewPagerBusNumber, list));
            mMagicIndicator.setNavigator(commonNavigator);
            ViewPagerHelper.bind(mMagicIndicator, mViewPagerBusNumber);
            if (position != 0) {
                mViewPagerBusNumber.setCurrentItem(position, true);
            }
            removeTopView(mLayoutContainer);
        }else{
            removeTopView(mLayoutContainer);
            addTopView(R.layout.view_bus_empty,mLayoutContainer);
        }
    }
    @OnClick(R2.id.btn_reset)
    public void onRefresh(){
        requestData();
    }
}